package com.bnz.service.impl;

import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentVo;
import com.bnz.service.StudentService;
import com.bnz.util.DbUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

import static com.bnz.util.DbUtils.getSqlSession;

public class StudentServiceImpl implements StudentService {
    @Override
    public List<Student> findAll() throws IOException {
        try (SqlSession sqlSession = getSqlSession()) {
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            ClassesMapper mapper1 = sqlSession.getMapper(ClassesMapper.class);
            List<Student> students = mapper.findAll();
            for (Student student : students) {

                String cname = mapper1.findById(student.getCid()).getCname();
                student.setCname(cname);
            }
            return students;
        }
    }

    // 2.分页查询
    @Override
    public PageResult<Student> findByPage(int page, int pageSize) throws IOException {
        // 2.1 设置分页参数
        PageHelper.startPage(page, pageSize);
        //2.2 开始分页查询
        try (SqlSession session = getSqlSession()) {
            StudentMapper studentMapper = session.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
            List<Student> students = studentMapper.findAll();
            for (Student student : students) {

                String cname = classesMapper.findById(student.getCid()).getCname();
                student.setCname(cname);
            }
            //2.3 将查询到的学生转化成page对象
            Page<Student> studentPage = (Page<Student>) students;

            // 2.4 返回PageResult对象
            return new PageResult<>(studentPage.getTotal(), studentPage.getResult());
        }

    }

    //3. 条件查询带分页
    @Override
    public PageResult<Student> search(int page, int pageSize, StudentVo studentVo) throws IOException {
        //3.1 设置分页参数
        PageHelper.startPage(page,pageSize);
        //3.2 开始分页查询
        try(SqlSession session = DbUtils.getSqlSession()){
            StudentMapper studentMapper = session.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
            //3.3 查询所有的学生
            List<Student> students = studentMapper.findByWords(studentVo);
            for (Student student : students) {
                String cname = classesMapper.findById(student.getCid()).getCname();
                student.setCname(cname);
            }
            //3.4 将查询得到的学生转换为page对象
            Page<Student> studentPage = (Page<Student>) students;
            System.out.println("studentPage.getTotal" +studentPage.getTotal());
            System.out.println("studentPage.getResult" +studentPage.getResult());
            //3.5 返回PageResult对象(参数一： 总记录数 参数二：每页记录集合 参数三：总页数 参数四：当前页
            return new PageResult<>(studentPage.getTotal(),studentPage.getResult(),studentPage.getPages(),page);
        }
    }

    @Override
    public void add(Student student) throws IOException {
        try (SqlSession session = DbUtils.getSqlSession()) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            mapper.insertStu(student);
        }
    }

    @Override
    public void updateById(Student student) throws IOException {
        try (SqlSession session = DbUtils.getSqlSession()) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            mapper.updateById(student);
        }
    }

    @Override
    public void delete(List<String> sids) throws IOException {
        try (SqlSession session = DbUtils.getSqlSession()) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            mapper.deleteByIds(sids);
        }
    }

    @Override
    public void insert(Student student) throws IOException {
        try (SqlSession session = DbUtils.getSqlSession()) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            mapper.insertStu(student);
        }
    }

    @Override
    public void deleteById(Integer sid) throws IOException {
        try (SqlSession sqlSession = DbUtils.getSqlSession()) {
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            mapper.deleteById(sid);
            System.out.println("编号为:"+sid+" 已完成删除~");
        }
    }


}
